package com.cheer.ba.presenter.impl;

import android.content.Context;

import com.cheer.ba.R;
import com.cheer.ba.constant.CodeStatus;
import com.cheer.ba.constant.ReturnCode;
import com.cheer.ba.http.BaseCallback;
import com.cheer.ba.http.CallManager;
import com.cheer.ba.http.HandleRetCode;
import com.cheer.ba.http.Http;
import com.cheer.ba.http.request.APIService;
import com.cheer.ba.model.MapModel;
import com.cheer.ba.model.ResponseData;
import com.cheer.ba.presenter.GeneralPresenter;
import com.cheer.ba.utils.NetUtils;
import com.cheer.ba.view.base.IViewController;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

/**
 * 地图数据处理
 *
 * @author HQ
 *         created at 2017/3/3 0003 15:50
 */
public class MapPresenter extends GeneralPresenter {

    private Context mContext;
    private IViewController mView;
    private APIService mCircleService;

    @Override
    public void attachView(IViewController view) {
        this.mView = view;
        mContext = getContext(view);
        mCircleService = Http.getCircleService(mContext);
    }

    /**
     * 查询油站地点地图信息
     */
    public void getOilAddress() {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, mContext.getString(R.string.no_net_work));
            return;
        }
        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, "");
        Call<ResponseData<List<MapModel>>> call = mCircleService.getOilAddress(2002, "");
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<List<MapModel>>>() {
            @Override
            public void onResponse(Call<ResponseData<List<MapModel>>> call, Response<ResponseData<List<MapModel>>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData<List<MapModel>> res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        List<MapModel> oilAddressList = res.getData();
                        if (null != oilAddressList) {
                            if (oilAddressList.isEmpty()) {
                                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_NULL, CodeStatus.Gegeneral.DATA_SUCCESS_NULL, mContext.getString(R.string.no_more_oil_address));
                            } else {
                                mView.updateView(oilAddressList);
                                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "");
                            }
                            return;
                        }
                    } else {
                        HandleRetCode.handler(mContext, res, true, mContext.getString(R.string.data_load_fail));
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "");
                        return;
                    }
                }
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, mContext.getString(R.string.data_load_fail));
            }

            @Override
            public void onFailure(Call<ResponseData<List<MapModel>>> call, Throwable t) {
                super.onFailure(call, t);
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, mContext.getString(R.string.data_load_fail));
            }
        });
    }

    @Override
    public void detachView() {
    }

    @Override
    public List<MapModel> getCacheData() {
        List<MapModel> maps = new ArrayList<MapModel>();
        MapModel model1 = new MapModel(30.62215, 114.358048, "怡景苑", "黄强", "12345678", "怡景苑", false, true, 1);
        MapModel model2 = new MapModel(30.510833, 114.408881, "珞瑜路726号世界城光谷步行街", "玩乐", "12345678", "珞瑜路726号世界城光谷步行街", false, true, 0.5);
        MapModel model3 = new MapModel(30.435921, 114.489884, "综合保税区", "公司", "12345678", "综合保税区", false, true, 1);
        MapModel model4 = new MapModel(30.506539, 114.458478, "武汉市东湖高新区荷叶山路与关东园五", "雷凯", "12345678", "武汉市东湖高新区荷叶山路与关东园五", false, true, 0.8);
        maps.add(model1);
        maps.add(model2);
        maps.add(model3);
        maps.add(model4);
        return maps;
    }
}
